<template>
  <div class="app-container">
    <el-button
      type="primary"
      style="margin-bottom: 10px"
      icon="el-icon-plus"
      @click="handleAdd"
      >添加配置</el-button
    >
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择">
          <el-option-group
            v-for="dict in dict.type.ly_group"
            :key="dict.value"
            :label="dict.label"
          >
            <el-option
              v-for="item in getProjectOption(
                groupAndProjectOptions,
                dict.value
              )"
              :key="item.projectId"
              :label="item.projectName"
              :value="item.projectId"
            >
            </el-option>
          </el-option-group>
        </el-select>
      </el-form-item>
      <el-form-item prop="examineProjectName">
        <el-input
          v-model="queryParams.examineProjectName"
          placeholder="检查内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="examineCateType">
        <el-select
          v-model="queryParams.examineCateType"
          placeholder="请选择分类"
          clearable
        >
          <el-option
            v-for="dict in dict.type.ly_inspection_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="configList"
      @selection-change="handleSelectionChange"
      :border="true"
    >
      <el-table-column label="ID" align="center" prop="examineConfigId" />
      <el-table-column
        label="自定义检查内容"
        align="center"
        prop="examineProjectTitle"
        :show-overflow-tooltip="true"
        width="150px"
      />
      <el-table-column
        label="检查内容"
        align="center"
        prop="examineProjectName"
        :show-overflow-tooltip="true"
        width="150px"
      />
      <el-table-column
        label="所属企业"
        align="center"
        prop="groupId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ly_group" :value="scope.row.groupId" />
        </template>
      </el-table-column>
      <el-table-column
        label="所属项目"
        align="center"
        prop="projectName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="分类"
        align="center"
        prop="examineCateType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.ly_inspection_type"
            :value="scope.row.examineCateType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="子类"
        align="center"
        prop="examineProjectType"
        width="150px"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.ly_examine_project_type"
            :value="scope.row.examineProjectType"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="默认值" align="center" prop="defaultValue">
        <template slot-scope="scope">
          {{ scope.row.defaultValue === 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column
        label="是否需要文字"
        align="center"
        prop="isNeedWords"
        width="150px"
      >
        <template slot-scope="scope">
          {{ scope.row.defaultValue === 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column
        label="是否需要附件"
        align="center"
        prop="isNeedFile"
        width="150px"
      >
        <template slot-scope="scope">
          {{ scope.row.defaultValue === 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="150px"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="120px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ctnm-ly:config:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡检配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      :center="true"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="120px"
        :show-message="false"
      >
        <el-form-item label="企业·项目" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择">
            <el-option-group
              v-for="dict in dict.type.ly_group"
              :key="dict.value"
              :label="dict.label"
            >
              <el-option
                v-for="item in getProjectOption(
                  groupAndProjectOptions,
                  dict.value
                )"
                :key="item.projectId"
                :label="item.projectName"
                :value="item.projectId"
              >
              </el-option>
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item label="巡检内容" prop="examineProjectName">
          <el-input v-model="form.examineProjectName" />
        </el-form-item>
        <el-form-item label="巡检自定义内容" prop="examineProjectTitle">
          <el-input
            v-model="form.examineProjectTitle"
            placeholder="例如：是否有xxx情况"
          />
        </el-form-item>
        <el-form-item label="巡检别名" prop="examineAlias">
          <el-input
            v-model="form.examineAlias"
            placeholder="格式：由大小写字母组成的6-30位字符"
          />
        </el-form-item>
        <el-form-item label="责任人" prop="dutyRemark">
          <el-input
            v-model="form.dutyRemark"
            placeholder="例如：张xx 186****6772"
          />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="默认值" prop="defaultValue">
              <el-select v-model="form.defaultValue">
                <el-option :key="1" :label="'正常'" :value="1" />
                <el-option :key="0" :label="'异常'" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="巡检分类" prop="examineCateType">
              <el-select v-model="form.examineCateType" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.ly_inspection_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否需要文字" prop="isNeedWords">
              <el-select v-model="form.isNeedWords">
                <el-option :key="1" :label="'是'" :value="1" />
                <el-option :key="0" :label="'否'" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否需要附件" prop="isNeedFile">
              <el-select v-model="form.isNeedFile">
                <el-option :key="1" :label="'是'" :value="1" />
                <el-option :key="0" :label="'否'" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status">
                <el-option :key="1" :label="'启动'" :value="1" />
                <el-option :key="0" :label="'禁用'" :value="0" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listConfig,
  getConfig,
  delConfig,
  addConfig,
  updateConfig,
  getAllGroupAndProjectApi,
} from "@/api/inspection/config";

export default {
  name: "Config",
  dicts: ["ly_inspection_type", "ly_group", "ly_examine_project_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检配置表格数据
      configList: [],
      // 企业项目数据列表
      groupAndProjectOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
        projectId: null,
        examineCateType: null,
        examineProjectName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [{ required: true, message: "不能为空", trigger: "blur" }],
        examineProjectName: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        examineProjectTitle: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        examineAlias: [{ required: true, trigger: "blur" }],
        dutyRemark: [{ required: true, trigger: "blur" }],
        examineCateType: [{ required: true, trigger: "blur" }],
        defaultValue: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        isNeedWords: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        isNeedFile: [
          {
            required: true,
            trigger: "blur",
          },
        ],
        createTime: [{ required: true, trigger: "blur" }],
        status: [
          {
            required: true,
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getAllGroupAndProject();
  },
  methods: {
    /** 查询巡检配置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then((response) => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAllGroupAndProject() {
      getAllGroupAndProjectApi().then((response) => {
        console.log(response.data);
        this.groupAndProjectOptions = response.data;
      });
    },
    getProjectOption(groupAndProjectOptions, groupId) {
      const data = groupAndProjectOptions.filter(
        (option) => option.groupId === groupId
      );
      return data.length ? data[0].projectList : [];
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        examineConfigId: null,
        groupId: null,
        projectId: null,
        examineAlias: null,
        examineCateType: null,
        examineProjectTitle: null,
        examineProjectName: null,
        examineProjectType: null,
        defaultValue: null,
        isNeedWords: null,
        isNeedFile: null,
        dutyRemark: null,
        sort: null,
        createTime: null,
        deleteTime: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.examineConfigId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const examineConfigId = row.examineConfigId || this.ids;
      getConfig(examineConfigId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.examineConfigId != null) {
            updateConfig(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const examineConfigIds = row.examineConfigId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除巡检配置编号为"' + examineConfigIds + '"的数据项？'
        )
        .then(function () {
          return delConfig(examineConfigIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ctnm-ly/config/export",
        {
          ...this.queryParams,
        },
        `config_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
